import 'package:flutter01/pages/sqfLite/utils/object_util.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

TimeOfDay? stringToTimeOfDay(String? string) {
  if (ObjectUtil.isEmptyString(string)) return null;
  List<String> items = string!.split(":");
  if (ObjectUtil.isEmptyList(items) || items.length != 2) return null;
  TimeOfDay? timeOfDay;
  try {
    timeOfDay =
        TimeOfDay(hour: int.parse(items[0]), minute: int.parse(items[1]));
  } catch (e) {}
  return timeOfDay;
}

//2020.10.25 10:10:01
DateTime? stringToDateTime(String dateString) {
  String formattedString = dateString.replaceAll(".", "-");
  DateTime? dateTime;
  try {
    dateTime = DateTime.parse(formattedString);
  } catch (e) {}
  return dateTime;
}

String? formatTime({String? formatter, DateTime? dateTime}) {
  if (dateTime == null) return null;
  String formatterString =
      ObjectUtil.isEmptyString(formatter) ? "yyyy.MM.dd HH:mm:ss" : formatter!;
  var dateFormatter = DateFormat(formatterString);
  String? dateTimeString;
  try {
    dateTimeString = dateFormatter.format(dateTime);
  } catch (e) {}
  return dateTimeString;
}

String? timeOfDayToString(TimeOfDay timeOfDay){
  int hour = timeOfDay.hour;
  int minute = timeOfDay.minute;
  String hourString = hour.toString();
  if(hour < 10){
    hourString = '0'+hourString;
  }
  String minuteString = minute.toString();
  if(minute < 10){
    minuteString = '0' + minuteString;
  }
  return hourString + ':' + minuteString;
}
